import axios from 'axios'

const instance = axios.create({
  baseURL: 'https://v1.itooi.cn',
  timeout: 5000
})

instance.interceptors.request.use(
  config => {
    return config
  },
  err => {
    return Promise.reject(err)
  }
)

instance.interceptors.response.use(
  response => {
    const res = response.data
    if (res.code === 200) {
      return res.data
    }
    return Promise.reject(new Error(res.msg))
  },
  error => {
    if (error.response.status === 404) {
      error.message = '请求路径找不到：404'
    } else {
      error.message = error.response.data.msg
    }
    return Promise.reject(error)
  }
)

export default instance
